<template>
  <div class="app-container" :size="size" >
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="属性" prop="attrId">
        <el-select v-model="queryParams.attrId">
          <el-option
            v-for="item in options.attrOptions"
            :key="item.attrId"
            :label="item.attrName"
            :value="item.attrId+''"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="属性键值" prop="valueKey">
        <el-input
          v-model="queryParams.valueKey"
          placeholder="请输入属性值键值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="属性值" prop="valueLabel">
        <el-input
          v-model="queryParams.valueLabel"
          placeholder="请输入属性值显示"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="delFlag">
        <el-select v-model="queryParams.delFlag" placeholder="属性状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button class="query-btn" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
        <el-button class="reset-btn" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          class="add-btn"
          plain
          icon="el-icon-plus"
          @click="handleAdd"
          v-hasPermi="['goods:attrval:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          class="edit-btn"
          plain
          icon="el-icon-edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['goods:attrval:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          class="del-btn"
          type="danger"
          plain
          icon="el-icon-delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['goods:attrval:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          class="download-btn"
          plain
          icon="el-icon-download"
          @click="handleExport"
          v-hasPermi="['goods:attrval:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border stripe v-loading="loading" :data="attrvalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="属性值id" align="center" prop="valueId" />
      <el-table-column label="属性类型" align="center" prop="attrName" />
      <el-table-column label="属性编码" align="center" prop="valueKey" />
      <el-table-column label="属性值" align="center" prop="valueLabel" />
      <el-table-column label="显示顺序" align="center" prop="orderNum" />
      <el-table-column label="是否默认" align="center" prop="isDefault">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isDefault"/>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="updateBy" />
      <el-table-column label="操作时间" align="center" prop="updateTime" width="160" />
      <el-table-column label="状态" align="center" prop="delFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.delFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            class="edit-btn"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['goods:attrval:edit']"
          >修改</el-button>
          <el-button
            class="del-btn"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['goods:attrval:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品属性值对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="属性id" prop="attrId">
          <el-select v-model="form.attrId" placeholder="请选择属性id">
            <el-option
              v-for="dict in dict.type.${dictType}"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item> -->
      <el-form-item label="属性" prop="attrId" >
        <el-select v-model:value="form.attrId" disabled >
          <el-option
            v-for="item in options.attrOptions"
            :key="item.attrId"
            :label="item.attrName"
            :value="item.attrId+''"
          />
        </el-select>
      </el-form-item>
        <el-form-item label="属性键值" prop="valueKey">
          <el-input v-model="form.valueKey" placeholder="请输入属性键值" />
        </el-form-item>
        <el-form-item label="属性值" prop="valueLabel">
          <el-input v-model="form.valueLabel" placeholder="请输入属性值" />
        </el-form-item>
        <el-form-item label="显示顺序" prop="orderNum">
          <el-input v-model="form.orderNum" type="number" placeholder="请输入显示顺序" />
        </el-form-item>
        <el-form-item label="是否默认" prop="isDefault">
          <el-radio-group v-model="form.isDefault">
            <el-radio
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="是否默认" prop="isDefault">
          <el-select v-model="form.isDefault" placeholder="请选择是否默认">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="状态" prop="delFlag">
          <el-radio-group v-model="form.delFlag">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="save-btn" @click="submitForm">确 定</el-button>
        <el-button class="cancel-btn" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAttrval, getAttrval, delAttrval, addAttrval, updateAttrval, findMaxOrderNum } from "@/api/goods/attrval";
import { optionselect as getAttrOptions } from "@/api/goods/attr";

export default {
  name: "AttrVal",
  dicts: ['sys_yes_no', 'sys_normal_disable'],
  computed: {
    size() {
      return this.$store.getters.size
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品属性值表格数据
      attrvalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 默认属性id
      defaultAttrId: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        attrId: null,
        valueKey: null,
        valueLabel: null,
        delFlag: null,
      },
      options: {
        attrOptions: []
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        attrId: [
          { required: true, message: "属性id不能为空", trigger: "change" }
        ],
        valueKey: [
          { required: true, message: "属性值键值不能为空", trigger: "blur" }
        ],
        valueLabel: [
          { required: true, message: "属性值显示不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    const attrId = this.$route.params && this.$route.params.attrId;
    this.getAttr(attrId);
  },
  methods: {
    getAttr(attrId) {
      this.getAttrList();
      this.queryParams.attrId = attrId;
      this.defaultAttrId = attrId;
      this.getList();
    },
    getAttrList() {
      getAttrOptions().then(res => {
        let data = res.data;
        data.filter(a => {
          if (a.valueUnit) {
            a.attrName = a.attrName + '【' + a.valueUnit + '】'
          }
        })
        this.options.attrOptions = data;
      })
    },
    /** 查询商品属性值列表 */
    getList() {
      this.loading = true;
      listAttrval(this.queryParams).then(response => {
        this.attrvalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      findMaxOrderNum(this.queryParams.attrId).then(res => {
        this.form = {
          valueId: null,
          attrId: this.queryParams.attrId,
          valueKey: null,
          valueLabel: null,
          orderNum: res.data,
          isDefault: 'N',
          delFlag: '0',
          remark: null
        };
        this.resetForm("form");
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.valueId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品属性值";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const valueId = row.valueId || this.ids
      getAttrval(valueId).then(response => {
        this.form = response.data;
        this.form.attrId = this.form.attrId+''
        this.open = true;
        this.title = "修改商品属性值";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.valueId != null) {
            updateAttrval(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAttrval(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const valueIds = row.valueId || this.ids;
      this.$modal.confirm('是否确认删除商品属性值编号为"' + valueIds + '"的数据项？').then(function() {
        return delAttrval(valueIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('goods/attrval/export', {
        ...this.queryParams
      }, `attrval_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
